package com.hxzon.aixzsecretary;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Timer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.hxzon.aixzsecretary.mail.SendMailFrame;
import com.hxzon.aixzsecretary.net.ChatServer;
import com.hxzon.aixzsecretary.net.FileServer;
import com.hxzon.aixzsecretary.net.UdpClient;
import com.hxzon.aixzsecretary.net.UdpQuit;
import com.hxzon.aixzsecretary.net.UdpServer;
import com.hxzon.aixzsecretary.net.XmlServer;
import com.hxzon.aixzsecretary.ui.AixzCalendar;
import com.hxzon.aixzsecretary.ui.BacklogList;
import com.hxzon.aixzsecretary.ui.UserList;

public class AixzSecretary {
	private UdpServer udpServer;
	private UdpClient udpClient;
	private AixzCalendar calendar;
	private UserList userList;
	private BacklogList backlogList;
	private JButton userButton;
	private JButton backlogButton;
	private JFrame frame;
	private JPanel cPane;

	private String myname;
	private String myIP;

	static {
		try {
			UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
			// UIManager.setLookAndFeel( "com.pagosoft.plaf.PgsLookAndFeel" );
			// UIManager.setLookAndFeel(
			// "com.nilo.plaf.nimrod.NimRODLookAndFeel" );
			// UIManager.setLookAndFeel( "com.jtattoo.plaf."
			// );//JTattooUtilitils
			// UIManager.setLookAndFeel("com.digitprop.tonic.TonicLookAndFeel");
			// not china
			// UIManager.setLookAndFeel(
			// "de.muntjak.tinylookandfeel.TinyLookAndFeel" );
			// not china
			// UIManager.setLookAndFeel("com.shfarr.ui.plaf.fh.FhLookAndFeel");
			// UIManager.setLookAndFeel(
			// "com.l2fprod.gui.plaf.skin.LinuxLookAndFeel");
			// UIManager.setLookAndFeel(
			// "com.easynth.lookandfeel.EaSynthLookAndFeel" );
			// not china
			// UIManager.setLookAndFeel( new NapkinLookAndFeel() );
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	public AixzSecretary() {//
		myname = System.getProperty("user.name");
		try {
			myIP = InetAddress.getLocalHost().getHostAddress();
			//System.out.println(myIP);
		} catch (UnknownHostException e) {
		}
		showface();
		new Timer().schedule(new SecretaryTimerTask(), new Date());
	}

	public void showface() {
		frame = new JFrame(myIP);
		cPane = (JPanel) frame.getContentPane();
		cPane.setLayout(null);

		new XmlServer().start();
		new ChatServer().start();
		new FileServer().start();

		udpServer = new UdpServer();
		udpClient = new UdpClient();

		calendar = new AixzCalendar();
		calendar.setBounds(2, 2, 240, 140);
		cPane.add(calendar);

		userList = new UserList();
		userList.observe(udpServer);
		userList.observe(udpClient);
		udpServer.start();
		udpClient.start();
		userList.addItem(myname + ":/" + myIP);
		userList.setBounds(2, 172, 240, 0);
		cPane.add(userList);


		backlogList = new BacklogList();
		backlogList.observe(calendar);
		backlogList.setBounds(2, 202, 240, 180);
		cPane.add(backlogList);

		backlogButton = new JButton("待办事项列表");
		backlogButton.setBounds(2, 172, 240, 30);
		cPane.add(backlogButton);
		backlogButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userList.setBounds(2, 172, 240, 0);
				backlogList.setBounds(2, 202, 240, 180);
				backlogButton.setBounds(2, 172, 240, 30);
			}
		});

		userButton = new JButton("在线用户列表");
		userButton.setBounds(2, 142, 240, 30);
		cPane.add(userButton);
		userButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userList.setBounds(2, 172, 240, 145);
				backlogList.setBounds(2, 202, 240, 0);
				backlogButton.setBounds(2, 317, 240, 30);
			}
		});

		JButton mailButton = new JButton("发送邮件");
		mailButton.setBounds(2, 385, 50, 30);
		cPane.add(mailButton);
		mailButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SendMailFrame();
			}
		});

		// udpServer.start();udpClient.start();
		String iconpath = AixzSecretary.class.getResource("/icon.jpg")
				.toString().substring(5);//cant gif
		Image iconImage = Toolkit.getDefaultToolkit().getImage(iconpath);
		TrayIcon trayIcon = new TrayIcon(iconImage);
		try {
			SystemTray.getSystemTray().add(trayIcon);
		} catch (AWTException e) {
		}
		trayIcon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (frame.isVisible() == true)
					frame.setVisible(false);
				else
					frame.setVisible(true);
			}
		});
		frame.setIconImage(iconImage);
		frame.setSize(250, 450);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// EXIT_ON_CLOSE
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				new UdpQuit().start();
				System.exit(0);
			}
		});

	}

	public static void main(String[] args) {
		new AixzSecretary();
	}
}
